package com.zyw.zhuangdian.service.impl;

import com.zyw.zhuangdian.constant.ServiceConstant;
import com.zyw.zhuangdian.dao.MenuDao;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.dto.MenuDto;
import com.zyw.zhuangdian.pojo.dto.PageDto;
import com.zyw.zhuangdian.pojo.in.MenuDtoIn;
import com.zyw.zhuangdian.pojo.out.MenuDtoOut;
import com.zyw.zhuangdian.service.MenuService;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MenuServiceImpl implements MenuService {
    private final Logger logger = LoggerFactory.getLogger(ProductionServiceImpl.class);

    @Autowired
    private MenuDao menuDao;

    @Override
    public Resp<Void> addMenu(MenuDtoIn in) {
        Resp<Void> resp = new Resp<>();

        menuDao.addMenu(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> updateMenu(MenuDtoIn in) {
        Resp<Void> resp = new Resp<>();

        menuDao.updateMenu(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> deleteMenu(MenuDtoIn in) {
        Resp<Void> resp = new Resp<>();

        in.setStatus(ServiceConstant.STATUS_INVALID);

        menuDao.updateMenu(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<List<MenuDto>> listMenus(MenuDtoIn in) {
        Resp<List<MenuDto>> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }

        int count = menuDao.listMenusCount(BeanConvertUtil.beanCovertMap(in));
        if (count > 0){
            List<Map> users = menuDao.listMenus(BeanConvertUtil.beanCovertMap(in));
            List<MenuDto> dtoList = BeanConvertUtil.covertBeanList(users, MenuDto.class);
            if (in.getLevel()==ServiceConstant.MENU_TOP){
                dtoList.sort(Comparator.comparingLong(MenuDto::getMid));
            }else if(in.getLevel()==ServiceConstant.MENU_SUB){
                dtoList.sort(Comparator.comparingLong(MenuDto::getBelongTo));
            }
            resp.setData(dtoList);
        }

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }

        return resp;
    }

    @Override
    public Resp<List<MenuDtoOut>> showMenus(MenuDtoIn in) {
        Resp<List<MenuDtoOut>> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }

        int count = menuDao.listMenusCount(BeanConvertUtil.beanCovertMap(in));
        if (count > 0){
            Map<Long,MenuDtoOut> map = new HashMap<>();
            List<Map> users = menuDao.listMenus(BeanConvertUtil.beanCovertMap(in));
            List<MenuDto> dtoList = BeanConvertUtil.covertBeanList(users, MenuDto.class);

            for(MenuDto dto:dtoList){
                //根据level 选择key
                long key = 0;
                if (dto.getLevel()==ServiceConstant.MENU_TOP){
                    key = dto.getMid();
                }else if (dto.getLevel()==ServiceConstant.MENU_SUB){
                    key = dto.getBelongTo();
                }
                // 初始化map
                if (!map.containsKey(key)){
                    MenuDtoOut o = new MenuDtoOut();
                    o.setSubMenu(new ArrayList<>());
                    map.put(key,o);
                }
                // 设置到对应的对象中
                if (dto.getLevel()==ServiceConstant.MENU_TOP){
                    MenuDtoOut out = map.get(dto.getMid());
                    out.setMid(dto.getMid());
                    out.setIcon(dto.getIcon());
                    out.setTitle(dto.getTitle());
                    out.setLevel(dto.getLevel());
                    out.setSeq(dto.getSeq());
                    out.setPrivilege(dto.getPrivilege());
                }else if (dto.getLevel()==ServiceConstant.MENU_SUB){
                    MenuDto out = new MenuDto();
                    out.setMid(dto.getMid());
                    out.setRoute(dto.getRoute());
                    out.setBelongTo(dto.getBelongTo());
                    out.setTitle(dto.getTitle());
                    out.setLevel(dto.getLevel());
                    out.setSeq(dto.getSeq());
                    out.setPrivilege(dto.getPrivilege());
                    map.get(dto.getBelongTo()).getSubMenu().add(out);
                }
            }
            // 排序
            count = map.size();
            List<MenuDtoOut> list = new ArrayList<>(map.values());
            list.sort(Comparator.comparingInt(MenuDtoOut::getSeq));
            list.forEach((a)->a.getSubMenu().sort(Comparator.comparingInt(MenuDto::getSeq)));
            resp.setData(list);
        }

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }

        return resp;
    }
}
